package com.auxgroup.hr.front.service.performance.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.auxgroup.hr.front.service.comm.SysDictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.commons.utils.Underline2Camel;
import com.auxgroup.hr.front.commons.utils.WebServiceClient;
import com.auxgroup.hr.front.commons.utils.WebServiceUtils;
import com.auxgroup.hr.front.dao.domain.JobCurrPrimary;
import com.auxgroup.hr.front.dao.domain.PsCEpAppItem;
import com.auxgroup.hr.front.dao.domain.PsCEpAppItemPrimary;
import com.auxgroup.hr.front.dao.domain.PsCEpAppItemR;
import com.auxgroup.hr.front.dao.domain.PsCEpAppItemRPrimary;
import com.auxgroup.hr.front.dao.domain.PsCEpAppRole;
import com.auxgroup.hr.front.dao.domain.PsCEpAppRolePrimary;
import com.auxgroup.hr.front.dao.domain.PsCEpAppSec;
import com.auxgroup.hr.front.dao.domain.PsCEpAppSecPrimary;
import com.auxgroup.hr.front.dao.domain.PsCEpAppSecR;
import com.auxgroup.hr.front.dao.domain.PsCEpAppSecRPrimary;
import com.auxgroup.hr.front.dao.domain.PsCEpDocVw;
import com.auxgroup.hr.front.dao.domain.PsCEpDocVwPrimary;
import com.auxgroup.hr.front.dao.domain.PsCEpPropsVw;
import com.auxgroup.hr.front.dao.domain.PsCEpPropsVwPrimary;
import com.auxgroup.hr.front.dao.domain.PsCListVw;
import com.auxgroup.hr.front.dao.domain.PsCListVwPrimary;
import com.auxgroup.hr.front.dao.domain.PsCWfEmpVw;
import com.auxgroup.hr.front.dao.repository.PsCEpAppItemRRepository;
import com.auxgroup.hr.front.dao.repository.PsCEpAppItemRepository;
import com.auxgroup.hr.front.dao.repository.PsCEpAppRoleRepository;
import com.auxgroup.hr.front.dao.repository.PsCEpAppSecRRepository;
import com.auxgroup.hr.front.dao.repository.PsCEpAppSecRepository;
import com.auxgroup.hr.front.dao.repository.PsCEpDocVwRepository;
import com.auxgroup.hr.front.dao.repository.PsCEpPropsVwRepository;
import com.auxgroup.hr.front.dao.repository.PsCListVwRepository;
import com.auxgroup.hr.front.dao.repository.PsCWfEmpVwRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.performance.PerformancePlanService;
import com.auxgroup.hr.front.ws.SoapXmlBuild;

/** 
 * @author lvliuhao 
 * @version 创建时间：2018年9月10日 下午2:18:18 
 * 类说明 
 */
@Service(value="performancePlanService")
public class PerformancePlanServiceImpl implements PerformancePlanService {
	
	@Autowired
	private PsCEpDocVwRepository psCEpDocVwRepository;
	
	@Autowired
	private PsCEpAppRoleRepository psCEpAppRoleRepository;
	
	@Autowired
	private PsCEpAppSecRRepository psCEpAppSecRRepository;
	
	@Autowired
	private PsCEpAppSecRepository psCEpAppSecRepository;
	
	@Autowired
	private PsCEpAppItemRRepository psCEpAppItemRRepository;
	
	@Autowired
	private PsCEpAppItemRepository psCEpAppItemRepository;
	
	@Autowired
	private PsCEpPropsVwRepository psCEpPropsVwRepository;
	
	@Autowired
	private PsCListVwRepository psCListVwRepository;
	
	@Autowired
	public PsCWfEmpVwRepository psCWfEmpVwRepository;
	
	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;
	
	@Autowired
	public SysDictService sysDictService;
	
	@Value("${ws.processService.approve.address}")
	private String processURL;
	
	@Value("${ws.processService.calcPoint.soapAction}")
	private String calcPointSoapAction;
	
	@Value("${ws.processService.epStatus.soapAction}")
	private String epStatusSoapAction;
	

	@Override
	public List<PsCEpDocVw> getepdocs(String oprid, String transNum,String queryKey,Sort sort) {
		Specification<PsCEpDocVw> spec = new Specification<PsCEpDocVw>() {
			@Override
			public Predicate toPredicate(Root<PsCEpDocVw> root, CriteriaQuery<?> query,
					CriteriaBuilder builder) {
				Predicate transNumEqul = builder.equal(root.get("cTransactionNbr"), transNum);
				Predicate opridEqul = builder.equal(root.get("oprid"),oprid);
				Predicate all = builder.and(transNumEqul,opridEqul);
				if(StringUtils.isNotBlank(queryKey)){
					return builder.and(all,builder.like(root.get("name"), "%"+queryKey+"%"));
				}
				return builder.and(all);
			}
		};
		return psCEpDocVwRepository.findAll(spec,sort);
	}
	
	
	@Override
	public List<List<PsCEpAppRole>> processMap(String appraisalId,String version,String epPhase) {
		PsCEpAppRole query = new PsCEpAppRole();
		query.setcEpAppraisalId(appraisalId);
		query.setVersion(new BigDecimal(version));
		query.setcEpPhase(epPhase);
		List<PsCEpAppRole> appRoles = psCEpAppRoleRepository.findAll(Example.of(query));
		if(appRoles!=null&&!appRoles.isEmpty()){
			Map<Integer, List<PsCEpAppRole>> respVOMap = new TreeMap<Integer, List<PsCEpAppRole>>(new MapKeyComparator());
			List<PsCEpAppRole> respVoList;
			for (PsCEpAppRole appRole : appRoles) {
				if(respVOMap.containsKey(appRole.getSeqnbr().intValue())){
					respVOMap.get(appRole.getSeqnbr().intValue()).add(appRole);
				}else{
					respVoList = new ArrayList<PsCEpAppRole>();
					respVoList.add(appRole);
					respVOMap.put(appRole.getSeqnbr().intValue(), respVoList);
				}
			}
			List<List<PsCEpAppRole>> respVOlists = new ArrayList<List<PsCEpAppRole>>(respVOMap.values());
			return respVOlists;
		}
		return null;
	}
	
	
	
	private List<PsCEpAppRole> getEpAppRoles(PsCEpDocVw docVw){
		PsCEpAppRole query = new PsCEpAppRole();
		query.setcEpAppraisalId(docVw.getcEpAppraisalId());
		query.setVersion(docVw.getVersion());
		query.setcEpPhase(docVw.getcEpPhase());
		query.setcEpTemplate(docVw.getcEpTemplate());
		return psCEpAppRoleRepository.findAll(Example.of(query));
	}
	
	
	@Override
	public PsCEpAppRole getEpAppRole(PsCEpDocVw docVw,String emplid){
		PsCEpAppRolePrimary primary = new PsCEpAppRolePrimary();
		primary.setcEpAppraisalId(docVw.getcEpAppraisalId());
		primary.setVersion(docVw.getVersion());
		primary.setcEpPhase(docVw.getcEpPhase());
		primary.setcEpTemplate(docVw.getcEpTemplate());
		primary.setSeqnbr(docVw.getSeqnbr());
		primary.setEmplid(emplid);
		primary.setEmplRcd(new BigDecimal(0));
		return psCEpAppRoleRepository.findOne(primary);
	}
	
	public class MapKeyComparator implements Comparator<Integer>{
	    @Override
	    public int compare(Integer int1, Integer int2) {
	        return int1.compareTo(int2);
	    }
	}

	@Override
	public PsCEpDocVw getepdoc(PsCEpDocVwPrimary primary) {
		return psCEpDocVwRepository.findOne(primary);
	}


	@Override
	public List<PsCEpAppSecR> getEpAppSecRs(PsCEpDocVw docVw) {
		PsCEpAppSecR query = new PsCEpAppSecR();
		query.setcEpAppraisalId(docVw.getcEpAppraisalId());
		query.setVersion(docVw.getVersion());
		query.setSeqnbr(docVw.getSeqnbr());
		query.setEmplid(docVw.getEmplid2());
		query.setEmplRcd(new BigDecimal(0));
		query.setcEpPhase(docVw.getcEpPhase());
		return psCEpAppSecRRepository.findAll(Example.of(query));
	}
	
	
	private PsCEpAppSecR getEpAppSecR(PsCEpDocVw docVw,String emplid,String sectionId) {
		PsCEpAppSecRPrimary primary = new PsCEpAppSecRPrimary();
		primary.setcEpSectionId(new BigDecimal(sectionId));
		primary.setcEpTemplate(docVw.getcEpTemplate());
		primary.setcEpAppraisalId(docVw.getcEpAppraisalId());
		primary.setVersion(docVw.getVersion());
		primary.setSeqnbr(docVw.getSeqnbr());
		primary.setEmplid(emplid);
		primary.setEmplRcd(new BigDecimal(0));
		primary.setcEpPhase(docVw.getcEpPhase());
		return psCEpAppSecRRepository.findOne(primary);
	}


	@Override
	public List<PsCEpAppSec> getEpAppSecs(PsCEpDocVw docVw) {
		List<PsCEpAppSecR> secrs = getEpAppSecRs(docVw);
		if(secrs==null){
			return new ArrayList<PsCEpAppSec>();
		}
		Set<BigDecimal> sectionIds = secrs.stream().map(PsCEpAppSecR::getcEpSectionId).collect(Collectors.toSet());
		Specification<PsCEpAppSec> spec = new Specification<PsCEpAppSec>() {
			@Override
			public Predicate toPredicate(Root<PsCEpAppSec> root, CriteriaQuery<?> query,
					CriteriaBuilder builder) {
				Predicate docidEqul = builder.equal(root.get("cEpAppraisalId"), docVw.getcEpAppraisalId());
				Predicate versionEqul = builder.equal(root.get("version"),docVw.getVersion());
				Predicate templateEqul = builder.equal(root.get("cEpTemplate"),docVw.getcEpTemplate());
				Predicate all = builder.and(docidEqul,versionEqul,templateEqul);
				if(!sectionIds.isEmpty()){
					return builder.and(all,root.get("cEpSectionId").in(sectionIds));
				}
				return builder.and(all);
			}
		};
		return psCEpAppSecRepository.findAll(spec);
	}
	
	@Override
	public PsCEpAppSec getEpAppSec(PsCEpDocVw docVw, String sectionId) {
		PsCEpAppSecPrimary primary = new PsCEpAppSecPrimary();
		primary.setcEpAppraisalId(docVw.getcEpAppraisalId());
		primary.setcEpSectionId(new BigDecimal(sectionId));
		primary.setcEpTemplate(docVw.getcEpTemplate());
		primary.setVersion(docVw.getVersion());
		return psCEpAppSecRepository.findOne(primary);
	}
	


	@Override
	public List<PsCEpAppItemR> getEpAppItemRs(PsCEpDocVw docVw,
			BigDecimal sectionId) {
		PsCEpAppItemR query = new PsCEpAppItemR();
		query.setcEpAppraisalId(docVw.getcEpAppraisalId());
		query.setVersion(docVw.getVersion());
		query.setSeqnbr(docVw.getSeqnbr());
		query.setEmplid(docVw.getEmplid2());
		query.setEmplRcd(new BigDecimal(0));
		query.setcEpSectionId(sectionId);
		query.setcEpTemplate(docVw.getcEpTemplate());
		query.setcEpPhase(docVw.getcEpPhase());
		return psCEpAppItemRRepository.findAll(Example.of(query));
	}
	
	@Override
	public PsCEpAppItemR getEpAppItemR(PsCEpDocVw docVw,
			BigDecimal sectionId, String catType, String itemId) {
		PsCEpAppItemRPrimary primary = new PsCEpAppItemRPrimary();
		primary.setcEpAppraisalId(docVw.getcEpAppraisalId());
		primary.setVersion(docVw.getVersion());
		primary.setSeqnbr(docVw.getSeqnbr());
		primary.setEmplid(docVw.getEmplid2());
		primary.setEmplRcd(new BigDecimal(0));
		primary.setcEpSectionId(sectionId);
		primary.setcEpTemplate(docVw.getcEpTemplate());
		primary.setcEpPhase(docVw.getcEpPhase());
		primary.setcEpCatType(catType);
		primary.setcEpCatItemId(itemId);
		return psCEpAppItemRRepository.findOne(primary);
	}


	@Override
	public PsCEpAppItem getEpAppItem(PsCEpDocVw docVw,
			BigDecimal sectionId, String catType, String catItemId) {
		PsCEpAppItemPrimary primary = new PsCEpAppItemPrimary();
		primary.setcEpAppraisalId(docVw.getcEpAppraisalId());
		primary.setVersion(docVw.getVersion());
		primary.setcEpTemplate(docVw.getcEpTemplate());
		primary.setcEpSectionId(sectionId);
		primary.setcEpCatType(catType);
		primary.setcEpCatItemId(catItemId);
		return psCEpAppItemRepository.findOne(primary);
	}


	@Override
	@Transactional
	public PsCEpAppItem saveEpAppItem(PsCEpDocVw docVw, PsCEpAppItem item,
			String sectionId,String score,String comments,String nowEmplid) {
		PsCEpAppItem nowItem = getEpAppItem(docVw, new BigDecimal(sectionId), item.getcEpCatType(), item.getcEpCatItemId());
		if(nowItem!=null){
			objectMapper.map(item, nowItem,"notCopyNull_epAppItem");
			//保存item
			psCEpAppItemRepository.save(nowItem);
			PsCEpAppItemR itemR = getEpAppItemR(docVw, new BigDecimal(sectionId), nowItem.getcEpCatType(), nowItem.getcEpCatItemId());
			setScoreValue(itemR, score, comments, docVw.getcEvaType(), nowItem.getcEpSectionType());
			psCEpAppItemRRepository.save(itemR);
			return nowItem;
		}
		item.setcEpAppraisalId(docVw.getcEpAppraisalId());
		item.setcEpSectionId(new BigDecimal(sectionId));
		item.setcEpTemplate(docVw.getcEpTemplate());
		item.setVersion(docVw.getVersion());
		//获取区段
		PsCEpAppSec sec = getEpAppSec(docVw, sectionId);
		if(sec==null){
			return null;
		}
		item.setcEpCatType(sec.getcEpCatType());
		item.setcEpSectionType(sec.getcEpSectionType());
		//获取itemid
		List<String> itemId = sysDictService.getStringFromSql("select 'MPI'||PS_C_EP_PERINDEX_SEQNO.Nextval from {}", new String[]{"dual"});
		item.setcEpCatItemId(itemId.get(0));
		//保存item
		ObjectFieldUtils.handleNullValue(item);
		psCEpAppItemRepository.save(item);
		//获取当前文档的角色
		List<PsCEpAppRole> roles = getEpAppRoles(docVw);
		for (PsCEpAppRole role : roles) {
			PsCEpAppSecR secr = getEpAppSecR(docVw, role.getEmplid(), sectionId);
			//判断角色对该区段是否有访问权限，如果有则新增一条itemR。
			if(secr!=null){
				PsCEpAppItemR itemR = new PsCEpAppItemR();
				objectMapper.map(item, itemR);
				itemR.setcEpPhase(docVw.getcEpPhase());
				itemR.setSeqnbr(docVw.getSeqnbr());
				itemR.setcEpRolename(role.getcEpRolename());
				itemR.setEmplid(role.getEmplid());
				itemR.setEmplRcd(role.getEmplRcd());
				itemR.setCreatedttm(new Timestamp(System.currentTimeMillis()));
				itemR.setLastupddttm(new Timestamp(System.currentTimeMillis()));
				if(nowEmplid.equals(role.getEmplid())){
					setScoreValue(itemR, score, comments, item.getcEpCatType(), sec.getcEpSectionType());
				}
				ObjectFieldUtils.handleNullValue(itemR);
				psCEpAppItemRRepository.save(itemR);
			}
		}
		return item;
	}
	
	
	@Override
	public List<PsCEpAppItem> getEpAppItems(PsCEpDocVw docVw,
			BigDecimal sectionId) {
		List<PsCEpAppItemR> itemRs = getEpAppItemRs(docVw, sectionId);
		if(itemRs==null||itemRs.isEmpty()){
			return null;
		}
		Set<String> catTypes = itemRs.stream().map(PsCEpAppItemR::getcEpCatType).collect(Collectors.toSet());
		Set<String> catItemIds = itemRs.stream().map(PsCEpAppItemR::getcEpCatItemId).collect(Collectors.toSet());
		
		Specification<PsCEpAppItem> spec = new Specification<PsCEpAppItem>() {
			@Override
			public Predicate toPredicate(Root<PsCEpAppItem> root, CriteriaQuery<?> query,
					CriteriaBuilder builder) {
				Predicate docidEqul = builder.equal(root.get("cEpAppraisalId"), docVw.getcEpAppraisalId());
				Predicate versionEqul = builder.equal(root.get("version"),docVw.getVersion());
				Predicate templateEqul = builder.equal(root.get("cEpTemplate"),docVw.getcEpTemplate());
				Predicate sectionIdEqul = builder.equal(root.get("cEpSectionId"),sectionId);
				Predicate all = builder.and(docidEqul,versionEqul,templateEqul,sectionIdEqul,root.get("cEpCatType").in(catTypes));
				return builder.and(all,root.get("cEpCatItemId").in(catItemIds));
			}
		};
		return psCEpAppItemRepository.findAll(spec);
	}


	@Override
	public String getItemOhterDescr(PsCEpAppItem item) {
		PsCEpPropsVwPrimary primary = new PsCEpPropsVwPrimary();
		primary.setcEpCatType(item.getcEpCatType());
		primary.setFieldnum(new BigDecimal(9));
		PsCEpPropsVw prop = psCEpPropsVwRepository.findOne(primary);
		if(prop==null){
			return "";
		}
		return getSpecilOtherDesc(prop,item);
	}
	
	@Override
	public String getSpecilOtherDesc(PsCEpPropsVw prop,PsCEpAppItem item){
		Set<String> filedNames = new HashSet<String>(Arrays.asList("C_PROMPT_1","C_PROMPT_2","C_PROMPT_3","C_PROMPT_4","C_PROMPT_5"));
		Object filedValue =  ObjectFieldUtils.getFieldValueByName(Underline2Camel.underline2Camel(prop.getFieldname(), true), item);
		if(filedNames.contains(prop.getFieldname())){
			if("C".equals(prop.getJpmPromptType())){
				PsCListVwPrimary listPrimary = new PsCListVwPrimary();
				listPrimary.setcListId(prop.getcListId());
				listPrimary.setcListValue(filedValue==null?"":filedValue.toString());
				PsCListVw listVw = psCListVwRepository.findOne(listPrimary);
				if(listVw==null){
					return "";
				}
				return listVw.getDescr100();
			}else{
				return filedValue==null?"":filedValue.toString();
			}
		}else if( prop.getFieldname().startsWith("C_YESNO")){
			if("Y".equals(filedValue)){
				return "是";
			}else{
				return "否";
			}
		}
		return filedValue==null?"":filedValue.toString();
	}


	@Override
	public List<PsCEpPropsVw> getItemProps(String catType) {
		PsCEpPropsVw query = new PsCEpPropsVw();
		query.setcEpCatType(catType);
		return psCEpPropsVwRepository.findAll(Example.of(query));
	}


	@Override
	public List<Map<String, Object>> buildRespItemDetail(List<PsCEpPropsVw> props,PsCEpAppItem item) {
		List<Map<String, Object>> respList = new ArrayList<Map<String, Object>>();
		setFirstDesrc(respList, props, item, "DESCR");
		Map<String, Object> resp;
		for (PsCEpPropsVw prop : props) {
			if("DESCR".equals(prop.getFieldname())||"EP_WEIGHT".equals(prop.getFieldname())){
				continue;
			}
			resp = new HashMap<String, Object>();
			resp.put("title", prop.getLabelName());
			resp.put("value", getSpecilOtherDesc(prop, item));
			respList.add(resp);
		}
		if("A".equals(item.getcEpSectionType())){
			setFirstDesrc(respList, props, item, "EP_WEIGHT");
		}
		return respList;
	}
	
	@Override
	public String getCommentScore(String evaType,PsCEpAppItemR itemR,String sectionType){
		if("A".equals(sectionType)){
			if("A".equals(evaType)){
				return String.valueOf(itemR.getcEpScore()==null||itemR.getcEpScore().equals(0)?"":itemR.getcEpScore());
			}
			if("B".equals(evaType)){
				return String.valueOf(StringUtils.isBlank(itemR.getcEpGrade())?"":itemR.getcEpGrade());
			}
		}
		if("B".equals(sectionType)){
			return String.valueOf(itemR.getcAddSubScore()==null||itemR.getcAddSubScore().equals(0)?"":itemR.getcAddSubScore());
		}
		return "";
	}
	
	/**
	 * 设置特殊位置的描述
	 * @param respList
	 * @param props
	 * @param item
	 * @param fileName
	 */
	private void setFirstDesrc(List<Map<String, Object>> respList,List<PsCEpPropsVw> props,PsCEpAppItem item,String fileName){
		PsCEpPropsVw needProp = null;
		for (PsCEpPropsVw prop : props) {
			if(fileName.equals(prop.getFieldname())){
				needProp = prop;
			}
		}
		Map<String, Object> resp = new HashMap<String, Object>();
		if(needProp==null&&fileName.equals("DESCR")){
			resp.put("title", "描述");
			Object fileValue = ObjectFieldUtils.getFieldValueByName(Underline2Camel.underline2Camel("DESCR", true), item);
			resp.put("value", fileValue);
		}else if(needProp==null&&fileName.equals("EP_WEIGHT")){
			resp.put("title", "权重");
			Object fileValue = ObjectFieldUtils.getFieldValueByName(Underline2Camel.underline2Camel("EP_WEIGHT", true), item);
			resp.put("value", fileValue);
		}else{
			resp.put("title", needProp.getLabelName());
			resp.put("value", getSpecilOtherDesc(needProp, item));
		}
		respList.add(resp);
	}



	@Override
	public void buildOldAssessInfo(List<Map<String, Object>> respVos,PsCEpDocVw docVw,PsCEpAppItem item) {
		PsCEpAppItemR itemR = this.getEpAppItemR(docVw, item.getcEpSectionId(), item.getcEpCatType(), item.getcEpCatItemId());
		if(itemR==null){
			return;
		}
		Specification<PsCEpAppItemR> spec = new Specification<PsCEpAppItemR>() {
			@Override
			public Predicate toPredicate(Root<PsCEpAppItemR> root, CriteriaQuery<?> query,
					CriteriaBuilder builder) {
				Predicate docidEqul = builder.equal(root.get("cEpAppraisalId"), docVw.getcEpAppraisalId());
				Predicate versionEqul = builder.equal(root.get("version"),docVw.getVersion());
				Predicate templateEqul = builder.equal(root.get("cEpTemplate"),docVw.getcEpTemplate());
				Predicate sectionIdEqul = builder.equal(root.get("cEpSectionId"),item.getcEpSectionId());
				Predicate epPhaseEqul = builder.equal(root.get("cEpPhase"),docVw.getcEpPhase());
				Predicate itemIdEqul = builder.equal(root.get("cEpCatItemId"),item.getcEpCatItemId());
				Predicate seqnbrLt = builder.lt(root.get("seqnbr"), itemR.getSeqnbr());
				return builder.and(itemIdEqul,docidEqul,versionEqul,templateEqul,sectionIdEqul,epPhaseEqul,seqnbrLt);
			}
		};
		List<PsCEpAppItemR> itemRs = psCEpAppItemRRepository.findAll(spec);
		if(itemRs!=null&&!itemRs.isEmpty()){
			Map<String, Object> respVo;
			for (PsCEpAppItemR R : itemRs) {
				//获取个人信息内容
				JobCurrPrimary primary = new JobCurrPrimary();
				primary.setEmplid(R.getEmplid());
				primary.setEmplRcd(R.getEmplRcd());
				PsCWfEmpVw emp = psCWfEmpVwRepository.findOne(primary);
				//评论结果、意见
				if("A".equals(item.getcEpSectionType())){
					respVo = new HashMap<String, Object>();
					respVo.put("title", "评估结果"+emp.getNameDisplay()+"("+R.getcEpRolename()+")");
					respVo.put("value", getCommentScore(docVw.getcEvaType(), R, item.getcEpSectionType()));
					respVos.add(respVo);
					respVo = new HashMap<String, Object>();
					respVo.put("title", "评估意见"+emp.getNameDisplay()+"("+R.getcEpRolename()+")");
					respVo.put("value", R.getComments());
					respVos.add(respVo);
				}
				//加减分
				if("B".equals(item.getcEpSectionType())){
					respVo = new HashMap<String, Object>();
					respVo.put("title", "加减分"+emp.getNameDisplay()+"("+R.getcEpRolename()+")");
					respVo.put("value", getCommentScore(docVw.getcEvaType(), R, item.getcEpSectionType()));
					respVos.add(respVo);
				}
			}
		}
	}


	@Override
	public PsCEpAppItemR saveEpAppItemR(PsCEpDocVw docVw, String comments,
			String score, String catType, String itemId,BigDecimal sectionId,String emplid) {
		PsCEpAppItemRPrimary primary = new PsCEpAppItemRPrimary();
		primary.setcEpAppraisalId(docVw.getcEpAppraisalId());
		primary.setVersion(docVw.getVersion());
		primary.setSeqnbr(docVw.getSeqnbr());
		primary.setEmplid(emplid);
		primary.setEmplRcd(new BigDecimal(0));
		primary.setcEpSectionId(sectionId);
		primary.setcEpTemplate(docVw.getcEpTemplate());
		primary.setcEpPhase(docVw.getcEpPhase());
		primary.setcEpCatType(catType);
		primary.setcEpCatItemId(itemId);
		PsCEpAppItemR itemR = psCEpAppItemRRepository.findOne(primary);
		PsCEpAppItem item = this.getEpAppItem(docVw, sectionId, catType, itemId);
		if(item==null){
			return null;
		}
		if(itemR==null){
			itemR = new PsCEpAppItemR();
			objectMapper.map(primary, itemR);
			itemR.setLastupddttm(new Timestamp(System.currentTimeMillis()));
		}
		setScoreValue(itemR, score, comments, docVw.getcEvaType(), item.getcEpSectionType());
		psCEpAppItemRRepository.save(itemR);
		return itemR;
	}
	
	/**
	 * 设置分数值
	 * @param itemR
	 * @param score
	 * @param comments
	 * @param evaType
	 * @param sectionType
	 */
	private void setScoreValue(PsCEpAppItemR itemR,String score,String comments,String evaType,String sectionType){
		if(StringUtils.isNotBlank(comments)){
			itemR.setComments(comments);
		}
		if("A".equals(sectionType)){
			if("A".equals(evaType)){
				itemR.setcEpScore(new BigDecimal(score));
			}
			if("B".equals(evaType)){
				itemR.setcEpGrade(score);
			}
		}else{
			itemR.setcAddSubScore(new BigDecimal(score));
		}
	}



	@Override
	@Transactional
	public void delSecItem(PsCEpDocVw docVw,String sectionId ,String catType, String itemId) {
		PsCEpAppItem item = this.getEpAppItem(docVw, new BigDecimal(sectionId), catType, itemId);
		if(item==null){
			return;
		}
		psCEpAppItemRepository.delete(item);
		PsCEpAppItemR query = new PsCEpAppItemR();
		query.setcEpCatType(catType);
		query.setcEpCatItemId(itemId);
		List<PsCEpAppItemR> itemRs = psCEpAppItemRRepository.findAll(Example.of(query));
		if(itemRs!=null&&!itemRs.isEmpty()){
			psCEpAppItemRRepository.delete(itemRs);
		}
	}


	@Override
	public Map<String, String> calcPoint(String docid, String version,
			String emplid) {
		StringBuilder soapXml = new StringBuilder();
		SoapXmlBuild.buildCalcPoint(soapXml, docid, version, emplid);
		String responXml = WebServiceClient.doPostSoap(processURL, soapXml.toString(), calcPointSoapAction);
		Map<String, String> result = WebServiceUtils.handleCalcPointRespXml(responXml, "C_EP_EVALUATE_S");
		return result;
	}


	@Override
	public PsCEpAppRole updAppRole(PsCEpDocVw docVw, String score, String revisedScore,
			String comments,String emplid,String type) {
		PsCEpAppRole role = getEpAppRole(docVw,emplid);
		if(role==null){
			return null;
		}
		if(StringUtils.isBlank(revisedScore)){
			revisedScore = score;
		}
		role.setComments(comments);
		if("A".equals(docVw.getcEvaType())){
			role.setcEpScore(new BigDecimal(score));
			role.setcEpScoreRevised(new BigDecimal(revisedScore));
		}else{
			role.setcEpGrade(score);
			role.setcEpGradeRevised(revisedScore);
		}
		//状态修改
		if(StringUtils.isNotBlank(type)&&"submit".equals(type)){
			role.setcEpAppStatus("B030");
		}
		role = psCEpAppRoleRepository.save(role);
		return role;
	}


	@Override
	public Map<String, String> changeDocStatus(String docid, String version) {
		StringBuilder soapXml = new StringBuilder();
		SoapXmlBuild.buildEpStatus(soapXml, docid, version);
		String responXml = WebServiceClient.doPostSoap(processURL, soapXml.toString(), epStatusSoapAction);
		Map<String, String> result = WebServiceUtils.handleEpStatustRespXml(responXml, "C_EP_STATUS_S");
		return result;
	}


	@Override
	public PsCEpDocVw getepdoc(String emplid, String transnum, String wftype) {
		PsCEpDocVw query = new PsCEpDocVw();
		query.setEmplid(emplid);
		query.setcTransactionNbr(transnum);
		query.setcWfType(wftype);
		List<PsCEpDocVw> docs = psCEpDocVwRepository.findAll(Example.of(query));
		if(docs!=null&&!docs.isEmpty()){
			return docs.get(0);
		}
		return null;
	}
}
 